﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace monitor_server
{
    public partial class Form10 : Form
    {
        form10enable d;
        string obl;
        string[] izmers;
        string[] izmers_m;
        string cur_izmer;
        bool exist = false;
        public Form10(form10enable sender1, string sender2, TreeNode sender3)
        {
            InitializeComponent();
            d = sender1;
            obl = sender2;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            d(false);
            this.Close();
        }

        private void Form10_Load(object sender, EventArgs e)
        {
            comboBox1.AutoCompleteMode = AutoCompleteMode.Append;
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            
            string[] split = new string[4];
            izmers_m = File.ReadAllLines(@"resources\mainIzmer.ini", Encoding.GetEncoding("Windows-1251"));
            for (int i = 0; i < izmers_m.Length; i++)
            {
                if (izmers_m[i][0] == '[')
                    continue;
                try
                {
                    split = izmers_m[i].Split('=');
                    if (split[0] == obl)
                    {
                        if (split[1] == "0")
                            exist = false;
                        else
                        {
                            cur_izmer = split[1];
                            exist = true;
                        }
                    }
                }
                catch (IndexOutOfRangeException)
                {
                    continue;
                }
            }

            izmers = File.ReadAllLines(@"resources\izmers.csv",Encoding.GetEncoding("Windows-1251"));
            for (int i = 0; i < izmers.Length; i++)
            {
                try
                {
                    split = izmers[i].Split(';');
                    if (split[3] == obl)
                    {
                        comboBox1.Items.Add(split[1]);
                    }
                }
                catch(IndexOutOfRangeException)
                {
                    continue;
                }
            }
            if (comboBox1.Items.Count < 1)
            {
                MessageBox.Show("В выбранной области нет активных измерителей");
                d(false);
                this.Close();
                return;
            }
            if(exist)
                comboBox1.SelectedItem = cur_izmer;
            this.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Выберите измеритель из списка.");
                return;
            }
            string newMain = (string)comboBox1.SelectedItem;

            if(Izmers.setMainIzmer(obl, newMain))
                MessageBox.Show("Головной измеритель успешно изменен.");
            else
                MessageBox.Show("Нет доступа к файлу mainIzmer.ini.");
            d(false);
            this.Close();
        }
        
    }
}
